﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using ENLibraryClass;

namespace web
{
    public partial class Formulario_web19 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Header.Title = "Citas";
            
            
            LNoEmpleado.Visible = false;
            LNoHora.Visible = false;


            if (Session["usuario"] != null)
            {
                if (DpVendedor.Items.Count == 0)
                {
                    ENEmpleados empleado = new ENEmpleados(0, "", "", "", "", "", "", "", "", "", "", "Vendedor", "", DateTime.Parse("01/01/1900"), "", "");
                    DataTable auxTabla = new DataTable();

                    auxTabla = empleado.BuscarEmpleadosPor();

                    int numFilas = auxTabla.Rows.Count;
                    string vendedor;

                    if (numFilas > 0)
                    {
                        DpVendedor.Items.Add("");
                        for (int i = 0; i < numFilas; i++)
                        {
                            vendedor = auxTabla.Rows[i].ItemArray[2].ToString() + " " + auxTabla.Rows[i].ItemArray[3].ToString();
                            DpVendedor.Items.Add(vendedor);
                        }
                    }
                }
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {

            DpHora.Items.Clear();


            string sFech;
            sFech = Calendar1.SelectedDate.Day + "/" + Calendar1.SelectedDate.Month + "/" + Calendar1.SelectedDate.Year + " 0:00:00";

            ENCitas auxCita = new ENCitas(0, DateTime.Parse(sFech), DateTime.Parse("01/01/1900"), 0, 0, "null", DateTime.Parse("01/01/1900"), DateTime.Parse("01/01/1900"));
            DataTable auxTabla = new DataTable();
            auxTabla = auxCita.BuscarCitasPor();

            DateTime hora = new DateTime();

            int numFilas=auxTabla.Rows.Count;
            if (numFilas > 0)
            {

                DpHora.Items.Add("8:00:00");
                DpHora.Items.Add("9:00:00");
                DpHora.Items.Add("10:00:00");
                DpHora.Items.Add("11:00:00");
                DpHora.Items.Add("12:00:00");
                DpHora.Items.Add("13:00:00");
                DpHora.Items.Add("16:00:00");
                DpHora.Items.Add("17:00:00");
                DpHora.Items.Add("18:00:00");
                DpHora.Items.Add("19:00:00");

                foreach (DataRow row in auxTabla.Rows)
                {

                    hora = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[2].ToString()));
                    string horafin;
                    horafin = hora.Hour.ToString() + ":00:00";

                    if (horafin == "8:00:00")
                    {
                        DpHora.Items.Remove("8:00:00");
                    }
                    else if (horafin == "9:00:00")
                    {
                        DpHora.Items.Remove("9:00:00");
                    }
                    else if (horafin == "10:00:00")
                    {
                        DpHora.Items.Remove("10:00:00");
                    }
                    else if (horafin == "11:00:00")
                    {
                        DpHora.Items.Remove("11:00:00");
                    }
                    else if (horafin == "12:00:00")
                    {
                        DpHora.Items.Remove("12:00:00");
                    }
                    else if (horafin == "13:00:00")
                    {
                        DpHora.Items.Remove("13:00:00");
                    }
                    else if (horafin == "16:00:00")
                    {
                        DpHora.Items.Remove("16:00:00");
                    }
                    else if (horafin == "17:00:00")
                    {
                        DpHora.Items.Remove("17:00:00");
                    }
                    else if (horafin == "18:00:00")
                    {
                        DpHora.Items.Remove("18:00:00");
                    }
                    else if (horafin == "19:00:00")
                    {
                        DpHora.Items.Remove("19:00:00");
                    }
                }
            }
            else
            {
                DpHora.Items.Add("8:00:00");
                DpHora.Items.Add("9:00:00");
                DpHora.Items.Add("10:00:00");
                DpHora.Items.Add("11:00:00");
                DpHora.Items.Add("12:00:00");
                DpHora.Items.Add("13:00:00");
                DpHora.Items.Add("16:00:00");
                DpHora.Items.Add("17:00:00");
                DpHora.Items.Add("18:00:00");
                DpHora.Items.Add("19:00:00");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            bool maloHora = false;
            bool maloEmpleado = false;



            string login=Session["usuario"].ToString();
            int idCliente=0;

            ENClientes cliente= new ENClientes(0, "", "", "", "", "", "", "", "", "", "", DateTime.Parse("01/01/1900"), "", "");
            DataTable clientes= new DataTable();

            clientes=cliente.BuscarClientesPor();
            int filasClientes=clientes.Rows.Count;
            if (filasClientes > 0)
            {
                idCliente = int.Parse(clientes.Rows[0].ItemArray[0].ToString());
            }
            else
            {
                maloHora = true;
            }


            int idEmpleado=0;
            string vendedor=DpVendedor.SelectedItem.ToString();
            int filasEmpleados = 0;
            
            if (vendedor == "")
            {
                ENEmpleados empleado = new ENEmpleados(0, "", "", "", "", "", "", "", "", "", "", "Vendedor", "", DateTime.Parse("01/01/1900"), "", "");
                DataTable empleados = new DataTable();

                empleados = empleado.BuscarEmpleadosPor();
                filasEmpleados = empleados.Rows.Count;

                if (filasEmpleados > 0)
                {
                    idEmpleado = int.Parse(empleados.Rows[0].ItemArray[0].ToString());
                }
                else
                {
                    maloEmpleado= true;
                }
            }
            else
            {
                string[] subcadenas = vendedor.Split(' ');

                string nombreEmpleado = subcadenas[0];
                string apellidoEmpleado = subcadenas[1];


                ENEmpleados empleado = new ENEmpleados(0, "", nombreEmpleado, apellidoEmpleado, "", "", "", "", "", "", "", "Vendedor", "", DateTime.Parse("01/01/1900"), "", "");
                DataTable empleados = new DataTable();

                empleados = empleado.BuscarEmpleadosPor();
                filasEmpleados = empleados.Rows.Count;
                if (filasEmpleados > 0)
                {
                    idEmpleado = int.Parse(empleados.Rows[0].ItemArray[0].ToString());
                }
                else
                {
                    maloEmpleado = true;
                }
            }


            if (maloEmpleado==false && maloHora==false)
            {
                string fecha = Calendar1.SelectedDate.Day + "/" + Calendar1.SelectedDate.Month + "/" + Calendar1.SelectedDate.Year + " 0:00:00";
                string hora = "01/01/1900 " + DpHora.SelectedItem.ToString();




                ENIdSiguiente idsiguiente = new ENIdSiguiente("citas", 0);
                int idCita = idsiguiente.BuscarIdSiguiente();

                ENCitas nuevaCita = new ENCitas(idCita, DateTime.Parse(fecha), DateTime.Parse(hora), idEmpleado, idCliente, tbMotivo.Text.ToString(), DateTime.Parse("01/01/1900"), DateTime.Parse("01/01/1900"));

                nuevaCita.AltaCita();

                idsiguiente.ActualizarIdSiguiente();

            }
            else
            {
                if (maloHora == true)
                {
                    LNoHora.Visible = true;
                }

                if (maloEmpleado == true)
                {
                    LNoEmpleado.Visible = true;
                }
            }
        }


    }
}
